import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import '../models/investment_data.dart';

class AssetBreakdownChart extends StatelessWidget {
  final AssetOverview overview;

  const AssetBreakdownChart({
    super.key,
    required this.overview,
  });

  @override
  Widget build(BuildContext context) {
    final assetTypes = overview.assetBreakdown.keys.toList();
    final totalValue = overview.totalValue;

    return Card(
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '资产分布',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 16),
            SizedBox(
              height: 200,
              child: PieChart(
                PieChartData(
                  sections: _createSections(assetTypes, totalValue),
                  centerSpaceRadius: 40,
                  sectionsSpace: 2,
                  pieTouchData: PieTouchData(
                    touchCallback: (FlTouchEvent event, pieTouchResponse) {
                      // 处理触摸事件
                    },
                  ),
                ),
              ),
            ),
            const SizedBox(height: 16),
            _buildLegend(assetTypes),
          ],
        ),
      ),
    );
  }

  List<PieChartSectionData> _createSections(
    List<AssetType> assetTypes,
    double totalValue,
  ) {
    final colors = [
      Colors.blue,
      Colors.green,
      Colors.orange,
      Colors.purple,
      Colors.red,
    ];

    return assetTypes.asMap().entries.map((entry) {
      final index = entry.key;
      final assetType = entry.value;
      final summary = overview.assetBreakdown[assetType]!;
      final percentage = summary.percentage;

      return PieChartSectionData(
        color: colors[index % colors.length],
        value: percentage,
        title: '${percentage.toStringAsFixed(1)}%',
        radius: 60,
        titleStyle: const TextStyle(
          fontSize: 12,
          fontWeight: FontWeight.bold,
          color: Colors.white,
        ),
        badgeWidget: _createBadge(assetType, summary.value),
        badgePositionPercentageOffset: 1.3,
      );
    }).toList();
  }

  Widget _createBadge(AssetType type, double value) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.3),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Text(
        '\$${value.toStringAsFixed(0)}',
        style: const TextStyle(
          fontSize: 10,
          fontWeight: FontWeight.bold,
          color: Colors.black87,
        ),
      ),
    );
  }

  Widget _buildLegend(List<AssetType> assetTypes) {
    final colors = [
      Colors.blue,
      Colors.green,
      Colors.orange,
      Colors.purple,
      Colors.red,
    ];

    return Wrap(
      spacing: 16,
      runSpacing: 8,
      children: assetTypes.asMap().entries.map((entry) {
        final index = entry.key;
        final assetType = entry.value;
        final summary = overview.assetBreakdown[assetType]!;

        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              width: 12,
              height: 12,
              decoration: BoxDecoration(
                color: colors[index % colors.length],
                shape: BoxShape.circle,
              ),
            ),
            const SizedBox(width: 4),
            Text(
              _getAssetTypeDisplayName(assetType),
              style: const TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.w500,
              ),
            ),
            Text(
              ' (${summary.count})',
              style: const TextStyle(
                fontSize: 12,
                color: Colors.grey,
              ),
            ),
          ],
        );
      }).toList(),
    );
  }

  String _getAssetTypeDisplayName(AssetType type) {
    switch (type) {
      case AssetType.nft:
        return 'NFT';
      case AssetType.stablecoin:
        return '稳定币';
      case AssetType.derivative:
        return '衍生品';
      case AssetType.other:
        return '其他';
    }
  }
}